<?php
$loader->requireOnce('includes/InPayment/InPaymentDecorator.class.php');
class ServicePayment extends InPaymentDecorator {
    public function __construct($objPayment) {
        $this->objPayment  =   $objPayment;
        parent::AbsInPayment($objPayment->getPatientId(), $objPayment->getInsuranceType());
    }

    public function calculatePayment(){
        $statusOpen =   Ticket::OPEN_STATUS;
    	$sql    =   "SELECT t.patient_id, SUM(ti.original_price) original_price, SUM(ti.insurance_price) insurance_price,
                        SUM(ti.patient_price) patient_price
                     FROM ticket t
                        LEFT JOIN ticket_item ti ON t.ticket_id=ti.ticket_id
                     WHERE t.status='$statusOpen' AND t.patient_id=$this->patientId
                     GROUP BY t.patient_id";
        $res    =   $this->_db->execute($sql);
        $paymentData    =   new InPaymentData();
        if($res && !$res->EOF){
        	$orgPrice  =   $res->fields['original_price'];
        	$insPrice  =   $res->fields['insurance_price'];
        	$patPrice   =   $res->fields['patient_price'];
        	if($this->insuranceType==self::INS_TYPE_VOLUNTEERED) {
        		$patPrice +=  (100-$this->volunteeredInsuranceSupport) * $insPrice/100;
        		$insPrice =   $this->volunteeredInsuranceSupport * $insPrice/100;
        	}
            $paymentData->setOrignalPrice($orgPrice);
            $paymentData->setInsurancePrice($insPrice);
            $paymentData->setPatientPrice($patPrice);
        }
        $paymentData->addDataFromObject($this->objPayment->calculatePayment());
        $paymentData->setInsuranceType($this->getInsuranceType());
        $paymentData->setPatientPrice($this->getPatientId());

        return  $paymentData;
    }

    public function calculateTicketPayment($ticketId) {
    	$itemType = Utils::getEnumKey(EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE,
            EnumerationConstant::IN_TICKE_ITEM_ITEMTYPE_SURGERY);
        $sql    =   "SELECT t.ticket_id, t.insurance, SUM(ti.original_price) original_price, SUM(ti.insurance_price) insurance_price,
                        SUM(ti.patient_price) patient_price
                     FROM in_ticket_item ti
                        INNER JOIN ticket t ON t.ticket_id=ti.ticket_id
                     WHERE t.ticket_id='$ticketId' AND ti.enum_item_type='$itemType'
                     GROUP BY t.ticket_id";
        $res    =   $this->_db->execute($sql);
        if($res===false) {
            throw new InPaymentException("Errors on query: '$sql'");
        }
        $paymentData    =   new InPaymentData();
        if($res && !$res->EOF){
            $orgPrice  =   $res->fields['original_price'];
            $insPrice  =   $res->fields['insurance_price'];
            $patPrice   =   $res->fields['patient_price'];
            if($res->fields['insurance']==Insurance::VOLUNTEERED) {
                $patPrice +=  (100-$this->volunteeredInsuranceSupport) * $insPrice/100;
                $insPrice =   $this->volunteeredInsuranceSupport * $insPrice/100;
            }
            $paymentData->setOrignalPrice($orgPrice);
            $paymentData->setInsurancePrice($insPrice);
            $paymentData->setPatientPrice($patPrice);
        }
        $paymentData->addDataFromObject($this->objPayment->calculateTicketPayment($ticketId));
        $paymentData->setInsuranceType($this->getInsuranceType());
        $paymentData->setPatientId($this->getPatientId());

        return  $paymentData;
    }
}
?>